package com.huanlis.cloud.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.server.ServerWebExchange;

/**
 * 网关跨域配置
 *
 * @author lihuan
 * @since 2023/10/20 14:25:13
 */
@Configuration
public class CorsWebConfiguration {

    /**
     * 配置网关跨域设置
     *
     * @author lihuan
     * @since 2023/10/20 14:32
     */
    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfigurationSource corsConfigurationSource = new CorsConfigurationSource() {
            @Override
            public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) {
                CorsConfiguration corsConfiguration = new CorsConfiguration();
                corsConfiguration.addAllowedHeader("*");
                corsConfiguration.addAllowedMethod("*");
                corsConfiguration.addAllowedOriginPattern("*");
                corsConfiguration.setAllowCredentials(true);
                corsConfiguration.setMaxAge(1800L);
                return corsConfiguration;
            }
        };
        return new CorsWebFilter(corsConfigurationSource);
    }

}
